package timesheet.service;
import java.text.ParseException;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import timesheet.model.Badging;
import timesheet.model.Event;
import timesheet.model.EventTime;
import timesheet.model.Project;
import timesheet.model.Users;
import timesheet.repository.UserRepository;

@Service
public class Batch {
	@Autowired	private UserRepository userRepository;
	@SuppressWarnings("deprecation")
	public void run() throws ParseException{
	
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("Connection");
	    EntityManager em = emf.createEntityManager();
	    em.getTransaction().begin();
		
		Users user = new Users("juju", "juju","admin", "M", "Ciarma", "Julien");
		java.util.Date start = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-06-04 08:00:00");
		java.util.Date stop = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-06-04 10:00:00");
		Project p = new Project("testnameproject");
		Event event = new Event(start,stop,"comment", "1", user, p);
		java.util.Date starttime1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-07-04 08:00:00");
		java.util.Date stopttime1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-07-04 10:00:00");
		Event event1 = new Event(starttime1,stopttime1,"comment2", "2", user, p);
		Date stopDate = new Date();
		stopDate.setHours(9);
		Badging b = new Badging(new Date(), stopDate, user);
		Date stopEvent = new Date();
		stopEvent.setHours(9);
		EventTime et = new EventTime(new Date(), stopEvent, event);
		em.persist(user);
		em.persist(event);
		em.persist(et);
		em.persist(event1);
		em.persist(p);
		em.persist(b);
	    em.getTransaction().commit();
	    em.close();
	    emf.close();
	}
}
